package com.dotcink.android.timer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{

	final static String TAG = "MainActivity";

	final static int TIME_PICKER = 1;

	// views
	Button startButton;
	Button resetButton;
	TextView timeDisplay;
	Animation buttonAnimation;

	Timer timer;
	WakeLock wakeLock;

	int defaultTime = 5 * 60;
	int time = defaultTime;
	
	int state;
	/**
	 * @state the beginning state
	 */
	final static int STATE_0 = 0;
	/**
	 * @state started
	 */
	final static int STATE_STARTED = 1;
	/**
	 * @state paused
	 */
	final static int STATE_PAUSED = 2;
	/**
	 * @state time is up
	 */
	final static int STATE_TIME_IS_UP = 3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// get views
		startButton = (Button) findViewById(R.id.start);
		resetButton = (Button) findViewById(R.id.reset);
		timeDisplay = (TextView) findViewById(R.id.display);

		buttonAnimation = (Animation) AnimationUtils.loadAnimation(this, R.anim.button);

		// set listeners
		startButton.setOnClickListener(startButtonListener);
		resetButton.setOnClickListener(resetButtonListener);
		((View)findViewById(R.id.desktop)).setOnClickListener(desktopListener);

		// create objects ...
		timer = new Timer();
		wakeLock = ((PowerManager)getSystemService(POWER_SERVICE))
				.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG);

		state = STATE_0;
		resetTime();
	}

	/**
	 * display the time remaining
	 * @param time the time to be displayed
	 */
	private void displayTime(int time){
		int minute = time / 60;
		int second = time % 60;

		timeDisplay.setText((minute<10 ? "0" : "") 
				+ minute 
				+ ":"
				+ (second<10 ? "0": "") 
				+ second);
	}

	/**
	 * reset the time to be defaultTime
	 */
	private void resetTime(){
		time = defaultTime;
		displayTime(time);
		
		if(state == STATE_TIME_IS_UP){
			state = STATE_0;
		}
	}
	// listeners
	private OnClickListener startButtonListener = new OnClickListener() {
		public void onClick(View v) {
			if(state == STATE_0 
					|| state == STATE_PAUSED
					|| state == STATE_TIME_IS_UP){

				Log.d(TAG, "Timer begins ...");

				state = STATE_STARTED;

				startButton.setText(getString(R.string.pause));

				startButton.startAnimation(buttonAnimation);
				resetButton.startAnimation(buttonAnimation);
				startButton.setVisibility(View.INVISIBLE);
				resetButton.setVisibility(View.INVISIBLE);

				wakeLock.acquire();

				timer.schedule(new CountdownTimerTask(), 0, 1000);

			}else if(state == STATE_STARTED){
				state = STATE_PAUSED;

				Log.d(TAG, "Timer pauses ..");

				startButton.setText(getString(R.string.resume));

				wakeLock.release();

				timer.cancel();
				timer = new Timer();

			}
		}
	};
	private OnClickListener resetButtonListener = new OnClickListener() {
		public void onClick(View v) {
			resetTime();
		}
	};
	private OnClickListener desktopListener = new OnClickListener() {
		public void onClick(View v) {
			if(startButton.getVisibility() == View.VISIBLE){
				startButton.startAnimation(buttonAnimation);
				startButton.setVisibility(View.INVISIBLE);
			}else{
				startButton.setVisibility(View.VISIBLE);
			}
			
			if(resetButton.getVisibility() == View.VISIBLE){
				resetButton.startAnimation(buttonAnimation);
				resetButton.setVisibility(View.INVISIBLE);
			}else{
				resetButton.setVisibility(View.VISIBLE);
			}
		}
	};

	// handlers
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			if(msg.what >= 0){
				displayTime(msg.what);
			}else{
				state = STATE_TIME_IS_UP;

				Log.d(TAG, "Time is up.");
				
				startButton.setText(getString(R.string.start));
				timeDisplay.setText(getString(R.string.time_is_up));

				wakeLock.release();

				timer.cancel();
				timer = new Timer();

				time = defaultTime;

			}
		};
	};

	private class CountdownTimerTask extends TimerTask{

		@Override
		public void run() {
			Message msg = new Message();
			msg.what = time;
			handler.sendMessage(msg);
			time--;
		}

	}

}